package com.jrx.anytxn.investor.timer.job.noticeRouteResult.config;

import com.jrx.anytxn.investor.constant.TransBizConstant;
import com.jrx.anytxn.investor.entity.TlFundRouteLog;
import com.jrx.anytxn.investor.timer.config.AnytxnBatchProperties;
import com.jrx.anytxn.investor.timer.job.noticeRouteResult.step.InvestorNoticeRouteResultProcessor;
import com.jrx.anytxn.investor.timer.job.noticeRouteResult.step.InvestorNoticeRouteResultWriter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 资金源路由通知
 *
 * @author liuyang
 * @date 2020-06-05
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class InvestorNoticeRouteResultConfiguration {
    @Resource(name = "bizSqlSessionFactory")
    private SqlSessionFactory sqlSessionFactory;
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;
    @Autowired
    private InvestorNoticeRouteResultProcessor noticeRouteResultProcessor;
    @Autowired
    private InvestorNoticeRouteResultWriter noticeRouteResultWriter;
    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @Bean
    public Job noticeRouteResultJob() {
        return jobBuilderFactory.get("noticeRouteResultJob")
                .start(noticeRouteResultStep())
                .build();
    }

    /**
     * 资金源路由通知流程处理
     *
     * @return Step
     * @author liuyang
     * @date 2020-06-05
     */
    @Bean
    public Step noticeRouteResultStep() {
        return stepBuilderFactory.get("noticeRouteResultStep")
                .<TlFundRouteLog, TlFundRouteLog>chunk(anytxnBatchProperties.getNoticeRouteResultConfig().getChunkLimit())//表示一批处理100条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(noticeRouteResultReader())
                .processor(noticeRouteResultProcessor)
                .writer(noticeRouteResultWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getNoticeRouteResultConfig().getThrottleLimit())//多线程
                .build();
    }

    /**
     * 查询未通知路由结果的资金源路由记录
     *
     * @return ItemReader<TlFundRouteLog> noticeRouteResultReader() 资金源路由记录
     * @author liuyang
     * @date 2020-06-05
     */
    @Bean
    @StepScope
    public SynchronizedItemStreamReader<TlFundRouteLog> noticeRouteResultReader() {

        MyBatisCursorItemReader<TlFundRouteLog> reader = new MyBatisCursorItemReader<>();
        reader.setSaveState(false);
        reader.setSqlSessionFactory(sqlSessionFactory);
        reader.setQueryId("com.jrx.anytxn.investor.mapper.ext.ExtTlFundRouteLogMapper.selectFundRouteByStatusForBatch");
        Map<String, Object> map = new HashMap<>();
        map.put("routeStatus", Arrays.asList(TransBizConstant.ROUTE_STATUS_T));
        reader.setParameterValues(map);

        SynchronizedItemStreamReader<TlFundRouteLog> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(reader);
        return synchronizedItemStreamReader;
    }
}
